package com.cy.lib.manager;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;

import com.cy.lib.util.SharedPreferencesUtil;

import java.util.Properties;
import java.util.UUID;

/**
 * @author CY
 * @title 账户管理
 * @date 2014-11-25
 * @package com.src.zf.doudou.manager
 */
public class AccountManager extends Manager {


    Context mContext;
    String mAppUUID;
    Account mAccount;

    protected AccountManager(Application context) {
        super();
        mContext = context;
        mAppUUID = genreateUUID(context);
    }

    public static AccountManager getInstance() {
        return (AccountManager) Manager.getInstance().getLocalService(LocalService.ACCOUNT);
    }

    public static String genreateUUID(Context context) {
        SharedPreferences sp = context.getSharedPreferences("appuuid", Context.MODE_PRIVATE);
        String uuid = sp.getString("appuuid", null);
        Log.e("", "uuid  " + uuid);
        if (uuid == null) {
            uuid = UUID.randomUUID().toString();
            sp.edit().putString("appuuid", uuid).commit();
        }
        return uuid;
    }

    public void store(Account account) {
        if (account == null) {
            throw new NullPointerException("store(Account account)");
        }
        new AsyncTask<Account, Object, Object>() {
            @Override
            protected Object doInBackground(Account... params) {
                Account account = params[0];
                Properties mProperties = new Properties();
                mProperties.setProperty(Account.KEY_USERNAME, account.username == null ? "" : account.username);
                mProperties.setProperty(Account.KEY_PASSWORD, account.password == null ? "" : account.password);
                mProperties.setProperty(Account.KEY_SAVEPWD, Boolean.toString(account.isSavePwd));
                SharedPreferencesUtil.saveObjByKey(mContext, mProperties, mAppUUID);
                mAccount = account;
                return null;
            }
        }.execute(account);

    }

    public void createAccout(String username, String password, boolean isSavePwd) {
        store(new Account(username, password, isSavePwd));
    }

    public Account getAccount() {
        if (mAccount == null) {
            Properties p = (Properties) SharedPreferencesUtil.readObjByKey(mContext, mAppUUID);
            if (p != null) {
                String k = p.getProperty(Account.KEY_SAVEPWD);
                mAccount = new Account(p.getProperty(Account.KEY_USERNAME), p.getProperty(Account.KEY_PASSWORD), Boolean.parseBoolean(k));
            } else {
                mAccount = new Account();
            }
        }
        return mAccount;
    }

    public boolean isRememberPwd() {
        return getAccount().isSavePwd;
    }

    public void setRememberPwd(boolean remember) {
        Account account = getAccount();
        account.isSavePwd = remember;
        store(account);
    }

    public void setRememberPwd(boolean remember, Account account) {
        account.isSavePwd = remember;
        store(account);
    }

    public void clearPwd() {
        Account account = getAccount();
        account.password = null;
        store(account);
    }

    public boolean isContainAccount() {
        return getAccount() != null;
    }

    public static class Account {
        private String username, password;
        private boolean isSavePwd;

        private Account() {
            // TODO Auto-generated constructor stub
        }

        public Account(String username, String password, boolean isSavePwd) {
            super();
            this.username = username;
            this.password = password;
            this.isSavePwd = isSavePwd;
        }

        public String getUsername() {
            return username;
        }

        public String getPassword() {
            return password;
        }

        public boolean isSavePwd() {
            return isSavePwd;
        }

        public static final String KEY_USERNAME = "username", KEY_PASSWORD = "password", KEY_SAVEPWD = "SAVEPWD";

    }
}
